package com.miluvs.contrller;

import com.miluvs.service.MiluvsService;
import io.milvus.client.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class MliuvsContrller {

    @Autowired
    private MiluvsService miluvsService;

    @GetMapping("/get")
    public  String  searchVectors(String name) {
        String collectionName = "data";
        int topK = 10;
        int nprobe = 16;
//        miluvsService.createCollection( collectionName);
//        miluvsService. createCollectTitle(collectionName);
//        miluvsService.hasCollection(collectionName);
//        miluvsService.addVectors(collectionName);
        SearchResponse searchResponse = miluvsService.searchVectors(collectionName,name);
        searchResponse.getResponse();
        List<List<Float>> resultDistancesList = searchResponse.getResultDistancesList();
        List<Float> floats = resultDistancesList.get(0);
        Float aFloat = floats.get(0);
        List<List<Long>> resultIdsList = searchResponse.getResultIdsList();
        List<Long> longs = resultIdsList.get(0);
        Long aLong = longs.get(0);
        if(aFloat==0){
            return  "您的回答暂时没有答案";
        }
        String prefix =null;
        if(aFloat>1){
            prefix ="您的最准确答案是:";
        }
        System.out.println(searchResponse.toString());
        Map<Long, String> map = getMap();
        String s = null;
        if(!StringUtils.isEmpty(aLong) ){
             s=map.get(aLong);
        }
        return prefix+ s;
    }
    public  static Map getMap() {
        Map<Long, String> map = new HashMap();
        map.put(1L, "你好呀");
        map.put(2L, "测试拉拉");
        map.put(3L, "标准");
        map.put(4L, "无线");
        map.put(5L, "表明");
        return map;
    }




}
